#### This R script reproduces figures in Online Appendix ####
#### Last edited: July 12, 2024			 ####
#### Amy Liu and Jay Kao             ####
#############################################################

rm(list=ls())



library(tidyverse)
library(readstata13)
library(cjoint)
library(cregg)

# load data
#setwd("C:/Users/XXXXXXXXXXXXX") #windows
data <- read.dta13("immigration.dta", nonint.factors = TRUE)

# Remove those respondents who do not complete conjoint choices
data <- data %>%
  filter(selected_immigration !="na", selected_nih !="na", selected_mother_tongue !="na")

data <- data %>%
  mutate(country=recode(country, 'No Info' = 'None-stated Country'),
         ethnic=recode(ethnic, 'No Info' = 'None-stated Ethnicity'),
         visa=recode(visa, 'No Info' = "None-stated Visa"),
         mandarin =recode(mandarin, 'No Info' = 'None-stated Mandarin'))


###############################
# Online Appendix Figure SI.2 #
###############################

# Difference in MMs by Linguistic Proficiency for National Origin 
data$mandarin2 <- relevel(data$mandarin, "Cannot Speak")

plot_lang_diff <- function(data, outcome, title) {
  f <- as.formula(paste(outcome, "~ country"))
  
  mm_diffs(data, f, ~ mandarin2, id = ~ record) |>
    dplyr::filter(BY == "Can Speak - Cannot Speak") |>
    plot() +
    theme_bw() +
    geom_text(aes(label = sprintf("%.2f", estimate)), vjust = -0.3) +
    scale_y_discrete(labels = c(
      "(country)" = "(Country of Origin)",
      "None-stated Country" = "No Info"
    )) +
    theme(legend.position = "none") +
    labs(
      title = title,
      x = "Estimated Difference (Can Speak - Cannot Speak)",
      y = NULL
    )
}

p_admission  <- plot_lang_diff(data, "selected_immigration",  "Admission")
p_nih        <- plot_lang_diff(data, "selected_nih",          "Health Insurance")
p_motherlang <- plot_lang_diff(data, "selected_mother_tongue","Mother Tongue")

p_admission
p_nih
p_motherlang


######################################
## Online Appendix Figures SI.3–SI.4 #
######################################


# --- Common axis labels for y ---
labels <- c(
  "(country)"              = "(Country of origin)",
  "None-stated Country"    = "No Info",
  "(ethnic)"               = "(Ethnicity)",
  "None-stated Ethnicity"  = "No Info",
  "(visa)"                 = "(Occupation/visa status)",
  "None-stated Visa"       = "No Info",
  "(mandarin)"             = "(Mandarin proficiency)"
)

# --- Create binary indicators (0 = No, 1 = Yes), then factorize as No/Yes ---
data <- data |>
  mutate(
    visit_sea_ever = case_when(
      visit_sea == 0 ~ 0L,
      visit_sea >= 1 & visit_sea <= 10 ~ 1L,
      TRUE ~ NA_integer_
    ),
    visit_mainland_ever = case_when(
      visit_mainland == 0 ~ 0L,
      visit_mainland >= 1 & visit_mainland <= 10 ~ 1L,
      TRUE ~ NA_integer_
    ),
    proficiency_seasia_ever = case_when(
      proficiency_seasia == 0 ~ 0L,
      proficiency_seasia >= 1 & proficiency_seasia <= 3 ~ 1L,
      TRUE ~ NA_integer_
    ),
    proficiency_english_ever = case_when(
      proficiency_english == 0 ~ 0L,
      proficiency_english >= 1 & proficiency_english <= 3 ~ 1L,
      TRUE ~ NA_integer_
    )
  ) |>
  mutate(across(
    c(visit_sea_ever, visit_mainland_ever, proficiency_seasia_ever, proficiency_english_ever),
    ~ factor(.x, levels = c(0, 1), labels = c("No", "Yes"))
  ))

# --- function for plots ---
mm_plot <- function(df, outcome, by_var, title, xlab) {
  f_outcome <- stats::as.formula(paste(outcome, "~ country + ethnic + visa + mandarin"))
  f_by      <- stats::as.formula(paste("~", by_var))
  
  mm_diffs(df, f_outcome, by = f_by, id = ~ record) |>
    plot() +
    theme_bw() +
    scale_y_discrete(labels = labels) +
    theme(legend.position = "none") +
    labs(title = title, x = xlab, y = NULL)
}

#################
## Figure SI.3
#################

# Panel 3a: Ever visited Southeast Asia?
xlab_sea <- "Estimated Difference (Visited SEA − Not Visited SEA)"
p_si3a_admission   <- mm_plot(data, "selected_immigration",  "visit_sea_ever",      "Admission",        xlab_sea)
p_si3a_nih         <- mm_plot(data, "selected_nih",          "visit_sea_ever",      "Health Insurance", xlab_sea)
p_si3a_instruction <- mm_plot(data, "selected_mother_tongue","visit_sea_ever",      "Mother Tongue",    xlab_sea)

print(p_si3a_admission)
print(p_si3a_nih)
print(p_si3a_instruction)

# Panel 3b: Ever visited China?
xlab_china <- "Estimated Difference (Visited China − Not Visited China)"
p_si3b_admission   <- mm_plot(data, "selected_immigration",  "visit_mainland_ever", "Admission",        xlab_china)
p_si3b_nih         <- mm_plot(data, "selected_nih",          "visit_mainland_ever", "Health Insurance", xlab_china)
p_si3b_instruction <- mm_plot(data, "selected_mother_tongue","visit_mainland_ever", "Mother Tongue",    xlab_china)

print(p_si3b_admission)
print(p_si3b_nih)
print(p_si3b_instruction)


#######################
## Figure SI.4
#######################

# Panel 4a: Speak a Southeast Asian language?
xlab_se_lang <- "Estimated Difference (Can speak SE language − Cannot)"
p_si4a_admission   <- mm_plot(data, "selected_immigration",  "proficiency_seasia_ever", "Admission",        xlab_se_lang)
p_si4a_nih         <- mm_plot(data, "selected_nih",          "proficiency_seasia_ever", "Health Insurance", xlab_se_lang)
p_si4a_instruction <- mm_plot(data, "selected_mother_tongue","proficiency_seasia_ever", "Mother Tongue",    xlab_se_lang)

print(p_si4a_admission)
print(p_si4a_nih)
print(p_si4a_instruction)


# Panel 4b: Speak English?
xlab_en <- "Estimated Difference (Can speak English − Cannot)"
p_si4b_admission   <- mm_plot(data, "selected_immigration",  "proficiency_english_ever", "Admission",        xlab_en)
p_si4b_nih         <- mm_plot(data, "selected_nih",          "proficiency_english_ever", "Health Insurance", xlab_en)
p_si4b_instruction <- mm_plot(data, "selected_mother_tongue","proficiency_english_ever", "Mother Tongue",    xlab_en)

print(p_si4b_admission)
print(p_si4b_nih)
print(p_si4b_instruction)

while (!is.null(dev.list())) dev.off()
